package com.example.learnvien.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.example.learnvien.R;
import com.example.learnvien.LearnViEnApplication;
import com.example.learnvien.assets.Assets;
import com.example.learnvien.data.LetterItemData;
import com.example.learnvien.data.NumberItemData;
import com.example.learnvien.utils.SoundUtil;

public class MainActivity extends Activity
{
	private static final int COLOR_BG_NORMAL = Color.TRANSPARENT;
	private static final int COLOR_BG_HIGHLIGHT = Color.YELLOW; 
	
	private boolean mIsLetter = true;

	private GridView mGridViewLetter;
	private GridViewLetterAdapter mLetterAdapter;
	
	private GridView mGridViewNumber;

	private ViewGroup mButtonVi;
	private ViewGroup mButtonEn;
	private ViewGroup mButtonSpeakWord;
	private ViewGroup mButtonSpeakLetter;
	
	private View mProgressView;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mProgressView = findViewById(R.id.layout_progress);
        mGridViewLetter = (GridView) findViewById(R.id.grid_view_letter);
        mGridViewNumber = (GridView) findViewById(R.id.grid_view_number);

        mButtonVi = (ViewGroup) findViewById(R.id.button_vi_layout);
        mButtonEn = (ViewGroup) findViewById(R.id.button_en_layout);
        mButtonSpeakLetter = (ViewGroup) findViewById(R.id.button_letter_layout);
        mButtonSpeakWord = (ViewGroup) findViewById(R.id.button_speak_word_layout);

        mProgressView.setVisibility(View.VISIBLE);
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                Assets.init();
                loadDone();
            }
        }).start();
	}

	private void initGridView()
	{
		updateLetterGridView();
        initNumberGridView();
        
		mGridViewLetter.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id)
			{
				LetterItemData data = (LetterItemData) parent.getItemAtPosition(position);
				if (mIsLetter)
				{
					SoundUtil.play(data.mSound);
				} else
				{
					SoundUtil.play(data.mSoundPhrase);
				}
			}
		});

		mGridViewNumber.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id)
			{
				NumberItemData data = (NumberItemData) parent.getItemAtPosition(position);
				if (Assets.sIsVi)
				{
					SoundUtil.play(data.mSoundVi);
				} else
				{
					SoundUtil.play(data.mSoundEn);
				}
			}
		});
	}

	private void initButtons()
	{
		mButtonVi.setBackgroundColor(COLOR_BG_HIGHLIGHT);
		mButtonEn.setBackgroundColor(COLOR_BG_NORMAL);
		mButtonSpeakLetter.setBackgroundColor(COLOR_BG_HIGHLIGHT);
		mButtonSpeakWord.setBackgroundColor(COLOR_BG_NORMAL);

		mButtonVi.getChildAt(0).setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				LearnViEnApplication.getInstance().changeLanguage(true);
				updateLetterGridView();
				initButtonsText();
				
				mButtonVi.setBackgroundColor(COLOR_BG_HIGHLIGHT);
				mButtonEn.setBackgroundColor(COLOR_BG_NORMAL);
			}
		});

		mButtonEn.getChildAt(0).setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				LearnViEnApplication.getInstance().changeLanguage(false);
				updateLetterGridView();
				initButtonsText();
				
				mButtonVi.setBackgroundColor(COLOR_BG_NORMAL);
				mButtonEn.setBackgroundColor(COLOR_BG_HIGHLIGHT);
			}
		});

		mButtonSpeakWord.getChildAt(0).setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				mIsLetter = false;

				mButtonSpeakLetter.setBackgroundColor(COLOR_BG_NORMAL);
				mButtonSpeakWord.setBackgroundColor(COLOR_BG_HIGHLIGHT);
			}
		});

		mButtonSpeakLetter.getChildAt(0).setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				mIsLetter = true;

				mButtonSpeakLetter.setBackgroundColor(COLOR_BG_HIGHLIGHT);
				mButtonSpeakWord.setBackgroundColor(COLOR_BG_NORMAL);
			}
		});

		initButtonsText();
	}

	private void initButtonsText()
	{
		((TextView) mButtonVi.getChildAt(0)).setText(Assets.Xml.Text.getCommonText(Assets.Xml.Text.LANGUAGE_VI));
		((TextView) mButtonEn.getChildAt(0)).setText(Assets.Xml.Text.getCommonText(Assets.Xml.Text.LANGUAGE_EN));
		((TextView) mButtonSpeakLetter.getChildAt(0)).setText(Assets.Xml.Text.getCommonText(Assets.Xml.Text.LETTER));
		((TextView) mButtonSpeakWord.getChildAt(0)).setText(Assets.Xml.Text.getCommonText(Assets.Xml.Text.WORD));
	}

	private void updateLetterGridView()
	{
	    if(mLetterAdapter == null)
		{
	        mLetterAdapter = new GridViewLetterAdapter(this,
	                Assets.Xml.LetterItem.getItems());
	        mGridViewLetter.setAdapter(mLetterAdapter);
		}
	    mLetterAdapter.updateDatas(Assets.Xml.LetterItem.getItems());
	}

    private void initNumberGridView()
    {
        GridViewNumberAdapter adapterNumber = new GridViewNumberAdapter(this,
				Assets.Xml.NumberItem.getItems());
		mGridViewNumber.setAdapter(adapterNumber);
    }

    private void loadDone()
    {
        runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                initGridView();
                initButtons();
                
                mProgressView.setVisibility(View.GONE);
            }
        });
    }
}
